<?php
/**
 *
 * @author Konovalov Maxim aka ZloY max.zloy@gmail.com
 * 
 */
class Zend_View_Helper_Rating {
	public $view;
	protected $_rating;
	protected $_itemId;
	protected $_itemType;
	public function rating($rating, $itemId, $itemType) {
		$this->_itemId = $itemId;
		$this->_itemType = $itemType;
		
		switch($rating)
		{
			case $rating < 0 : $this->_rating = "-" . $rating; break;
			case $rating > 0 : $this->_rating = "+" . $rating; break;
			default :  $this->_rating = $rating;
		}
		
		$rating = new Model_Rating(null, $this->_itemType);
		if(Zend_Auth::getInstance()->getIdentity()){
			if($mark = $rating->isVoted($this->_itemId, Zend_Auth::getInstance()->getIdentity()->id)){
				switch ((int) $mark){
					case 0: return $this->_getInActive(); break;
					case $mark < 0 : return $this->_getMinusRating();
					case $mark > 0 : return $this->_getPlusRating();
				}
			} else {
				return $this->_getInActive(true);	
			}
		} else {
			return $this->_getInActive();
		}		
	}
	
	protected function _getInActive($canVote = false)
	{
		if($canVote){
			return '<div class="rating"> 
					<a href="/rating/set/item_id/' . $this->_itemId . '/item_type/' . $this->_itemType . '/mark/1/" class="link-up-g rat">&nbsp;</a> 
					<a href="/rating/set/item_id/' . $this->_itemId . '/item_type/' . $this->_itemType . '/mark/0/" class="rat-b rat">' . $this->_rating . '</a> 
					<a href="/rating/set/item_id/' . $this->_itemId . '/item_type/' . $this->_itemType . '/mark/-1/" class="link-down-g rat">&nbsp;</a> 
				</div>';	
		 } else {
			return '<div class="rating"> 
					<a href="#" class="link-up-g">&nbsp;</a> 
					<a href="#" class="rat-b">' . $this->_rating . '</a> 
					<a href="#" class="link-down-g">&nbsp;</a> 
				</div>';
		}
	}
	
	protected function _getMinusRating()
	{
		return '<div class="rating"> 
					<a href="#" class="link-up-g">&nbsp;</a> 
					<a href="#" class="rat-r">' . $this->_rating . '</a> 
					<a href="#" class="link-down-r">&nbsp;</a> 
				</div>';
	}
	
	protected function _getPlusRating(){
		return '<div class="rating"> 
					<a href="#" class="link-up-green">&nbsp;</a> 
					<a href="#" class="rat-g">' . $this->_rating . '</a> 
					<a href="#" class="link-down-g">&nbsp;</a> 
				</div>';		
	}
	
	public function setView(Zend_View_Interface $view) {
		$this->view = $view;
	}
}